package org.abettor.tanqiu.gameloop;

import org.abettor.tanqiu.gameloop.object.Ball;
import org.abettor.tanqiu.gameloop.object.BallMap;
import org.abettor.tanqiu.gameloop.object.RailBallContainer;

import android.util.Log;

import com.badlogic.gdx.math.Vector2;

public class Simulation {
	public Simulation(){
		cantainer = new RailBallContainer();
		Ball tmpBall = new Ball(Ball.BALL_BACK,new Vector2(50,10));
		tmpBall.setStatus(Ball.BALL_STATUS_INRAIL_F);
		tmpBall.setRailVelocity(7000);
		cantainer.addHeadBall(tmpBall); 
	}
	private BallMap map;//地图
	private RailBallContainer cantainer;//球的容器
	/**
	 * 更新的时间
	 * @param updateTime
	 */
	public void update(float updateTime){
		int size = cantainer.size();
		for(int i = 0; i < size;i++){
			Ball tmpBall = cantainer.get(i);
			Vector2 pos = tmpBall.getPosition();
			Vector2 nextPos = map.nextPosition(pos, tmpBall.getRailVelocity(),0,updateTime);
			tmpBall.setPosition(nextPos);
		}
//		//进行球推着走的操作
//		for(int i=0; i < size;i++){
//			Ball tmpBall = cantainer.get(i);
//			Vector2 pos = tmpBall.getPosition();
//			if(i + 1 < size){
//				Ball tmpBall1 = cantainer.get(i+1);
//				if(checkBallCollsion(tmpBall,tmpBall1)){
//					Vector2 pos1 = map.nextNeiborPosition(pos);
//					Log.i("pos", "("+pos.x+","+pos.y+")("+pos1.x+","+pos.y+")");
//					tmpBall1.setPosition(pos1);
//				}
//			}
//		}
	}
	/**
	 * 检查两个球是否碰撞
	 * @param tmpBall
	 * @param tmpBall1
	 * @return
	 */
	public  static boolean checkBallCollsion(Ball tmpBall,Ball tmpBall1){
		Vector2 pos1 = tmpBall.getPosition();
		Vector2 pos2 = tmpBall1.getPosition();
		if(pos1.dst(pos2) < Constants.BALL_RAIDUS*2){
			return true;
		}
		return false;
	}
	
	public BallMap getMap() {
		return map;
	}
	public void setMap(BallMap map) {
		this.map = map;
	}
	public RailBallContainer getCantainer() {
		return cantainer;
	}
	public void setCantainer(RailBallContainer cantainer) {
		this.cantainer = cantainer;
	}
}
